﻿<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>API</title>
    <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="../bootstrap/css/pub.css" rel="stylesheet" />
    <script src="../bootstrap/js/jquery.min.js"></script>
    <script src="../bootstrap/js/bootstrap.min.js"></script>
    <script src="../bootstrap/js/bootstrap-paginator.js"></script>
    <!--[if lt IE 9]>
        <script src="bootstrap/js/html5shiv.min.js"></script>
        <script src="bootstrap/js/respond.min.js"></script>
    <![endif]-->
    <script src="../config.js"></script>
    <script src="../bootstrap/js/Common.js"></script>
    <script src="../bootstrap/js/controls.js"></script>
    <script type="text/javascript">
        var varType = 1;
        var relFilter = 1;
        $(function () {
            init(varType,relFilter,1);
        });
        function init(vtype,rel,pgindex) {
            if (vtype==1) {
                //菜单
                var menus = [
                    { name: "新增", action: "openAddWin()", css: "btn-primary" },
                    { name: "编辑", action: "openEditWin()", css: "btn-primary" }
                ];
                $(".menu-container").menu({ items: menus });
                //列表
                var columns = [
                    {
                        name: "编号", field: "id"
                    },
                    {
                        name: "数据类型", field: "type", formatter: function (value) {
                            if (value == 0) {
                                return "默认";
                            } else if (value == 1) {
                                return "开关";
                            } else if (value == 2) {
                                return "整型";
                            } else if (value == 3) {
                                return "浮点型";
                            } else if (value == 4) {
                                return "字符串";
                            } else if (value == 5) {
                                return "对象";
                            } else if (value == 6) {
                                return "数组";
                            }
                        }
                    },
                    { name: "描述", field: "description" },
                    { name: "已关联接入点", field: "accessPointID"},
                    { name: "已关联报警策略", field: "triggerRuleId"},
                    {
                        name: "关联",
                        field: "dataPointID",
                        title: '关联',
                        formatter: function (value, dataRow) {
                            return '<a href="javascript:void(0);" onclick="AssociatedTodevice(\'' +
                                dataRow.dataPointID +
                                '\')" class="btn btn-sm btn-default" style="background-color: #d9edf7;" ><i class="fa fa-trash-o"></i>关联接入点</a>' +
                                '&nbsp&nbsp&nbsp<a href="javascript:void(0);" onclick="RelatedToAcceRemove(\'' +
                                dataRow.dataPointID + '\',\'' + dataRow.accessPointID +'\')" class="btn btn-sm btn-default" style="background-color: #d9edf7;" ><i class="fa fa-trash-o"></i>解除关联</a>' +
                                '&nbsp&nbsp&nbsp<a href="javascript:void(0);" onclick="AssociatedToTrigger(\'' +
                                dataRow.dataPointID +
                                '\')" class="btn btn-sm btn-default" style="background-color: #d9edf7;" ><i class="fa fa-trash-o"></i>关联报警策略</a>' +
                                '&nbsp&nbsp&nbsp<a href="javascript:void(0);" onclick="RelatedToTrigRemove(\'' +
                                dataRow.dataPointID + '\',\'' + dataRow.triggerRuleId +'\')" class="btn btn-sm btn-default" style="background-color: #d9edf7;" ><i class="fa fa-trash-o"></i>解除关联</a>';
                        }
                    },
                    {
                        name: "操作",
                        field: "id",
                        title: '操作',
                        formatter: function (value,dataRow) {
                            return '<a href="javascript:void(0);" onclick="ResetVariable(\'' +
                                dataRow.dataPointID +
                                '\')" class="btn btn-sm btn-info" ><i class="fa fa-trash-o"></i>复位</a>' +
                                '&nbsp&nbsp&nbsp<a href="javascript:void(0);" onclick="openExstrWin(\'' +
                                value +
                                '\',\'' + dataRow.writeable +'\')" class="btn btn-sm btn-info" ><i class="fa fa-trash-o"></i>写入</a>';
                        }
                    }
                ];
            }
            else {
                //菜单
                var menus = [
                    { name: "新增", action: "openAddWin()", css: "btn-primary" },
                    { name: "编辑", action: "openEditWin()", css: "btn-primary" },
                    { name: "删除", action: "openDelWin()", css: "btn-primary" }
                ];
                $(".menu-container").menu({ items: menus });
                //列表
                var columns = [
                    {
                        name: "编号", field: "id"
                    },
                    {
                        name: "名称", field: "dataPointID"
                    },
                    {
                        name: "数据类型", field: "type", formatter: function (value) {
                            if (value == 0) {
                                return "默认";
                            } else if (value == 1) {
                                return "开关";
                            } else if (value == 2) {
                                return "整型";
                            } else if (value == 3) {
                                return "浮点型";
                            } else if (value == 4) {
                                return "字符串";
                            } else if (value == 5) {
                                return "对象";
                            } else if (value == 6) {
                                return "数组";
                            }
                        }
                    },
                    {
                        name: "变量类型", field: "modifier",
                        formatter: function (value) {
                            if (value == 0) {
                                return "系统变量";
                            } else if (value == 1) {
                                return "设备变量";
                            } else if (value == 2) {
                                return "内存变量";
                            }
                        }
                    },
                    { name: "描述", field: "description" }, {
                        name: "屏蔽状态", field: "state", formatter: function (value) {
                            if (value == 0) {
                                return "显示";
                            } else {
                                return "屏蔽";
                            }
                        }
                    },
                    { name: "默认值", field: "defaultValue" },
                    { name: "已关联报警策略", field: "triggerRuleId" },
                    {
                        name: "关联",
                        field: "dataPointID",
                        title: '关联',
                        formatter: function (value, dataRow) {
                            return '<a href="javascript:void(0);" onclick="AssociatedToTrigger(\'' +
                                dataRow.dataPointID +
                                '\')" class="btn btn-sm btn-default" style="background-color: #d9edf7;" ><i class="fa fa-trash-o"></i>关联至报警策略</a>' +
                                '&nbsp&nbsp&nbsp<a href="javascript:void(0);" onclick="RelatedToTrigRemove(\'' +
                                dataRow.dataPointID +
                                '\',\'' + dataRow.triggerRuleId + '\')" class="btn btn-sm btn-default" style="background-color: #d9edf7;" ><i class="fa fa-trash-o"></i>解除策略关联</a>';
                        }
                    },
                    {
                        name: "操作",
                        field: "id",
                        title: '操作',
                        formatter: function (value, dataRow) {
                            return '<a href="javascript:void(0);" onclick="ResetVariable(\'' +
                                dataRow.dataPointID +
                                '\')" class="btn btn-sm btn-info" ><i class="fa fa-trash-o"></i>复位</a>';
                        }
                    }
                ];

            }
            var param = { filter: $("#search").val(), type: vtype, filed: rel, pagesize: pagesize, pageindex: pgindex };
            $("#tab").datagrid({ columns: columns, dblclick: openEditWin }).loadData(host_name + "varDeclares/list", "post", param, function () {
                $.ajax({
                    url: host_name + "varDeclares/count?filter=" + vtype + "&filed=" + rel,
                    type: "get",
                    success: function (data) {
                        if (data < 10) {
                            $("#example").hide();
                        }
                        else {
                            $("#example").show();
                            var totalcount = Math.ceil(data / pagesize);
                            $('#pageLimit').bootstrapPaginator({
                                currentPage: pgindex,
                                totalPages: totalcount,//总页数
                                numberOfPages: pagenum,//显示页数
                                size: "normal",
                                bootstrapMajorVersion: 3,
                                alignment: "right",
                                itemTexts: function (type, page, current) {
                                    switch (type) {
                                        case "first": return "首页";
                                        case "prev": return "上一页";
                                        case "next": return "下一页";
                                        case "last": return "末页";
                                        case "page": return page;
                                    }//默认显示的是第一页。
                                },
                                onPageClicked: function (event, originalEvent, type, page) {//给每个页眉绑定一个事件，其实就是ajax请求，其中page变量为当前点击的页上的数字。
                                    init(vtype, rel,page);
                                }
                            });
                        }
                    }
                })
            });
        }
        //关联至接入点
        function AssociatedTodevice(val) {
            openModalWin("数据点关联接入点", "databindaccess.html?mode=1&dataPointID=" + val, refresh, 1000, 650, true);
        }
        //解除接入点关联
        function RelatedToAcceRemove(val, aId) {
            if (aId != "null" && aId != "undefined") {
                var delUrl = host_name + "accesspointmap/remove?Id=" + val;
                if (confirm("确定要解除该数据点和接入点[" + aId+"]的关联吗？")) {
                    $.ajax({
                        url: delUrl,
                        type: "get",
                        success: function (data) {
                            refresh();
                        }
                    });
                }
            }
            else {
                alert("当前数据点还未关联接入点！");
            }
        }
        //关联至报警策略
        function AssociatedToTrigger(val) {
            openModalWin("数据点关联报警策略", "databindtrigger.html?mode=1&dataPointID=" + val, refresh, 1000, 650, true);
        }
        //解除报警策略关联
        function RelatedToTrigRemove(val,tId)
        {
            if (tId != "null" && tId != "undefined") {
                var delUrl = host_name + "variabletrigger/remove?Id=" + val;
                if (confirm("确定要解除该数据点和报警策略[" + tId+"]的关联吗？")) {
                    $.ajax({
                        url: delUrl,
                        type: "get",
                        success: function (data) {
                            refresh();
                        }
                    });
                }
            }
            else {
                alert("当前数据点还未关联接入点！");
            }
        }
        //复位
        function ResetVariable(val) {
            $.ajax({
                url: host_name + "variabletrigger/reset",
                type: "get",
                data: { "id": val },
                success: function (data) {
                    if (data== "OK") {
                        alert("复位成功！");
                    }
                    else {
                        alert("复位失败！");
                    }
                },
                error: function (request) {
                    alert("连接失败");
                }
            });

        }
        //新增
        function openAddWin() {
            openModalWin("新增数据点", "datapointedit.html?mode=1", refresh, 700, 600);
        }
        //编辑
        function openEditWin() {
            var row = $("#tab").datagrid().getSelected();
            if (row.length > 0) {
                openModalWin("编辑数据点", "datapointedit.html?mode=2&id=" + row[0].id, refresh, 700, 600);
            }
            else {
                alert("请选择要编辑的数据！");
            }
        }
        //删除
        function openDelWin() {
            var row = $("#tab").datagrid().getSelected();
            if (row.length > 0) {
                var delUrl = host_name + "varDeclares/delete?Id=" + row[0].id;
                if (confirm("确定要删除选定的数据吗？")) {
                    $.ajax({
                        url: delUrl,
                        type: "get",
                        success: function (data) {
                            refresh();
                        }
                    });
                }
            } else {
                alert("请选择要删除的数据！");
            }
        }
        //数据点写入数据
        function openExstrWin(val,check) {
            if (check=="true") {
                openModalWin("数据点数据写入", "datapointexstr.html?id=" + val, refresh, 700, 300);
            }
            else {
                alert("当前数据点不可写入！");
            }
        }
        //按变量是否关联接入点搜索
        function onConditionChange(obj)
        {
            relFilter= $(obj).val();
            init(1, relFilter, 1);
        }
        //按类型显示变量
        function changeType(val) {
            varType = val;
            if (varType==1) {
                $("#conditionFilter").css('display', 'block'); 
                $(".selector").val("0");
            }
            else {
                $("#conditionFilter").css('display', 'none'); 
            }
            document.getElementById("search").value = "";
            init(varType,1,1);
        }
        //搜索
        function search()
        {
          init(varType, relFilter, 1);
        }
        //刷新
        function refresh() {
            $("#tab").datagrid().refresh();
        }
        //重置
        function reset() {
            document.getElementById("search").value = "";
            $(".selector").val("0");
            relFilter = 1;
            init(varType, relFilter, 1);
        }
    </script>
</head>
<body>
    <div class="container-fluid">
        <div class="row" style="margin-bottom:10px">
            <div class="col-md-2">
                <h4>数据点列表</h4>
            </div>
            <div class="col-md-10">
                <div class="menu-container pull-right">
                </div>
            </div>
        </div>
        <div class="row" style="margin-bottom:10px">
            <div class="col-md-2">
                <ul class="nav nav-pills">
                    <li class="active" id="li1">
                        <a href="javascript:void(0)" data-toggle="tab" onclick="changeType(1)">IO变量</a>
                    </li>
                    <li id="li2">
                        <a href="javascript:void(0)" data-toggle="tab" onclick="changeType(0)">内存变量</a>
                    </li>
                </ul>
            </div>
            <div class="col-md-2" id="conditionFilter">
                <select class="form-control" id="relatedCondition" onchange="onConditionChange(this)">
                    <option value="0" style="color: #b6b6b6" disabled selected>--按关联情况筛选--</option>
                    <option value="1">全部IO变量</option>
                    <option value="2">已关联接入点</option>
                    <option value="3">未关联接入点</option>
                </select>

            </div>
            <div class="col-md-4">
                <input type="text" id="search" value="" class="form-control input-xlarge" placeholder="输入描述搜索...">
            </div>
            <div class="col-md-4">
                <button class="btn btn-default" type="button" id="btnSearch" onclick="search()">搜索</button>
                <button class="btn btn-default" type="button" id="btnReset" onclick="reset()">重置</button>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12 table-responsive">
                <table id="tab" class="table table-bordered table-hover"></table>
            </div>
        </div>
        <div class="row" style="margin-right: 10px;">
            <div id="example" style="text-align: right">
                <ul id="pageLimit"></ul>
            </div>
        </div>
    </div>
</body>
</html>
